<?php

namespace common\models;

use common\helpers\commonApi;
use common\helpers\TransformApi;
use Yii;

/**
 * This is the model class for table "{{%goods}}".
 *
 * @property int $id
 * @property string|null $name 名称
 * @property string|null $main_img 主图
 * @property string|null $img 图片
 * @property string|null $feature 特点
 * @property float|null $price 价格
 * @property int|null $sales_volume 销售量
 * @property string|null $details 详情
 * @property string|null $create_time 创建时间
 * @property string|null $update_time 修改时间
 * @property int|null $views 浏览量
 * @property int|null $status 状态
 * @property int|null $sort 排序
 * @property int|null $delete 删除
 * @property int|null $classify_id 分类id
 */
class Goods extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%goods}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['feature', 'details'], 'string'],
            [['price'], 'number'],
            [['sales_volume', 'views', 'status', 'sort','delete','classify_id'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['name', 'main_img', 'img'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'main_img' => '主图',
            'img' => '图片',
            'feature' => '特点',
            'price' => '价格',
            'sales_volume' => '销售量',
            'details' => '详情',
            'create_time' => '创建时间',
            'update_time' => '修改时间',
            'views' => '浏览量',
            'status' => '状态',
            'sort' => '排序',
            'delete' => '删除',
            'classify_id' => '分类id',
        ];
    }
    public function getSpec()
    {
        return $this->hasMany(GoodsSpec::className(),['g_id'=>'id']);
    }

    public function calculate(array $goods,$g_id,$spec_id,$userid,$num)
    {
        $huiyuan = false;
        $shop_discount = System::getModel()->shop_discount??0;
        if ($shop_discount>0){
            $huiyuan = (new User())->isHuiyuan($userid,'','',0);
        }
        foreach ($goods as &$good){
            if (empty($good['g_id'])){
                return commonApi::message('商品编号不存在',false);
            }
            if (empty($good['spec_id'])){
                return commonApi::message('规格编号不存在',false);
            }
            if (empty($good['num'])){
                return commonApi::message('请选择购买数量',false);
            }
            if (!preg_match("/^[1-9][0-9]*$/" ,$good['num'])){
                return commonApi::message('购买数量必须是整数',false);
            }
            $good['mark'] = $good['g_id'].'_'.$good['spec_id'];
        }
        $markArr = array_column($goods,null,'mark');
        $goods_id = array_column($goods,'good_id');
        $spec_id = array_column($goods,'spec_id');


        $goods = Goods::find()->where(['id'=>$g_id,'status'=>1])->exists();
        if (!$goods){
            return commonApi::message('商品不存在',false);
        }
        $shop_discount = System::getModel()->shop_discount??0;
        $spec = GoodsSpec::find()->where(['g_id'=>$g_id,'id'=>$spec_id])->one();
        if (!$spec){
            return commonApi::message('规格不存在',false);
        }
        $total_amount = $spec->price*$num;
        if ($shop_discount>0){
            $huiyuan = (new User())->isHuiyuan($userid,'','',0);
            if ($huiyuan){
                $spec->price = $spec->price*($shop_discount/10);
                $spec->price = TransformApi::decimalPoint($spec->price);
            }
        }
        $pay_amount = $spec->price*$num;
        $discount_amount = $total_amount-$pay_amount;
        return commonApi::message('成功',true,['total_amount'=>$total_amount,'pay_amount'=>$pay_amount,'discount_amount'=>$discount_amount]);
    }
    
    public function buy($userid,$g_id,$spec_id,$num)
    {
        $res = $this->calculate($userid,$g_id,$spec_id,$num);
        if (!$res['success']){
            return $res;
        }

    }

    public function dataAssemble($goods)
    {

    }
}
